home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / MWCC03.ZIP / HOTKEY1.ZIP / HOTKEY1.PAS < prev    next >
Pascal/Delphi Source File  |  1993-08-18  |  24KB  |  707 lines

  1. {**********************************************************************}
  2. {*                                                                    *}
  3. {*          Microworks Sample Application                                        *}
  4. {*                                                                    *}
  5. {*         for Borland Pascal v7.0 and Turbo Pascal for Windows v1.5           *}
  6. {*                                                                    *}
  7. {*     Copyright 1992-93 Jeff Franks (Microworks) Sydney, Australia.  *}
  8. {*                                                                    *}
  9. {*         You are free to use, modify, reproduce and distribute the      *}
  10. {*         Sample Files (and/or any modified version) in any way you      *}
  11. {*         find useful.                                                                *}
  12. {*                                                                    *}
  13. {**********************************************************************}
  14.  
  15. {*** Introduction
  16.  
  17.     Application    := Application specific hotkey edit control
  18.  
  19.     Files          := Hotkey1.exe, Hotkey1.pas, Hotkey1.res
  20.  
  21.     Units Required := MObjects and MWCC.dll
  22.  
  23.     Purpose        := HotKey1 shows you how to,
  24.  
  25.                                      1. Install a 'task' hook using the new 'SetWindowHookEx' function.
  26.  
  27.                                      2. Set up the associated application 'Callback' function.
  28.  
  29.                                      3. Set up an application HotKey.
  30.  
  31.                                      4. Use some of the objects in the MWCC library.
  32.  
  33.     Tabs           := 2
  34.  
  35.     Screen         := 800 * 600
  36.  
  37.     Date           := August, 1993.
  38.  
  39.     Hotkey1 is a translation of the C program 'Shortcut Key Edit Sample' available on the
  40.     MSDNLIB forum as 4-52.zip. I translated the program in the hope of sovling a problem. It
  41.     didn't solve my problem but it did provide a useful utility. This is my first attempt at
  42.     translating a C program - What a nightmare!
  43.  
  44.     I have kept this translation true to the original. The only change apart from the OWL
  45.     conversion is the use of my own custom contols. The MWCC object units and library do not
  46.     require BWCC.DLL or Ctl3D.DLL.
  47.  
  48.     The Hotkey in this program is only valid for the application. When the application
  49.     looses the focus the Hot key doesn't work. The other example 'Hotkey2'
  50.     is my own work. It installs a system wide hot key that works whether the application
  51.     has the focus or not.
  52.  
  53.     I hope you find HotKey1 useful.
  54.  
  55.     Jeff...
  56.  
  57. ***}
  58.  
  59. program HotKey1;
  60.  
  61. {$R HotKey1.Res}
  62. {$C Demandload Moveable Discardable}
  63.  
  64. uses WinTypes, WinProcs, Strings, Win31, MObjects,
  65.          {$IFDEF Ver15}
  66.              WObjects;
  67.          {$ELSE}
  68.              Objects, OWindows, ODialogs;
  69.          {$ENDIF}
  70.  
  71. const
  72.  
  73.     {*** Window ID's ***}
  74.     idw_timer                       = 101;
  75.  
  76.     {*** Dialog ID's ***}
  77.     idd_HotKeyEdit                  = 401;
  78.     idd_Static1                     = 402;
  79.     idd_Static2                     = 403;
  80.     idd_Static3                     = 404;
  81.     idd_Install                     = 405;
  82.  
  83.     {*** Menu ID's ***}
  84.     idm_About                       = 701;
  85.     idm_EditInstallHotKey           = 702;
  86.     idm_RemoveHotKey                = 703;
  87.  
  88.     {*** User defined messages ***}
  89.     wm_Hotkey                       = wm_User + 1;
  90.     wm_InstallHotKey                = wm_User + 2;
  91.  
  92.     {*** far declaration for Callback function ***}
  93.     pToHotKeyHookProc : TFarProc    = nil;
  94.  
  95.     {*** Speaks for itself ***}
  96.     AppName : PChar                 = 'HotKey1';
  97.  
  98. type
  99.  
  100.     THotKeyStruct = record
  101.         wMenuKey          : Word;
  102.         wControlKey       : Word;
  103.         wShiftKey         : Word;
  104.         wKey              : Word;
  105.         wKeyNum           : Word;
  106.         szText, szKeyName : array[0..30] of Char;
  107.     end;
  108.  
  109.     PAboutDialog = ^TAboutDialog;
  110.     TAboutDialog = object(TSFXDialog)
  111.         {***
  112.  
  113.             TMWCCBmpButton is a BWCC style bitMap button object. TMWCCStatic is a static object
  114.             that displays either raised, recessed or normal static controls. WMDrawItem is required
  115.             to draw the TMWCCBmpButton ownerdraw button object.
  116.  
  117.         ***}
  118.         OkBut    : PMWCCBmpButton;
  119.         ST1, ST2 : PMWCCStatic;
  120.         constructor Init (AParent: PWindowsObject; AName: PChar);
  121.         procedure SetUpWindow; virtual;
  122.         procedure WMDrawItem (var Msg: TMessage); virtual wm_First + wm_DrawItem;
  123.     end;
  124.  
  125.     PHotKeyEdit = ^THotKeyEdit;
  126.     THotKeyEdit = object(TEdit)
  127.         {***
  128.  
  129.             If you make THotKeyEdit a descendant of TMWCCEdit you will get a recessed edit control.
  130.  
  131.         ***}
  132.         procedure DefWndProc (var Msg : TMessage); virtual;
  133.         procedure DisplayHotKey (var wModifierKey : Word);
  134.         procedure ResetHotKey; virtual;
  135.         procedure EraseHotKey; virtual;
  136.     end;
  137.  
  138.     PHotKeyEditDialog = ^THotKeyEditDialog;
  139.     THotKeyEditDialog = object(TSFXDialog)
  140.         {***
  141.  
  142.             THotKeyEditDialog is where you enter the Hotkey combination. It is a descendant of
  143.             TSFXDialog (SpecialFX Dialog) which is one of the MWCC custom objects. SFX objects
  144.             don't support menu's or TScroller scroll bars.
  145.  
  146.         ***}
  147.         EC            : PHotKeyEdit;
  148.         ST1, ST2, ST3 : PMWCCStatic;
  149.         But1, But2    : PMWCCBmpButton;
  150.         constructor Init(AParent: PWindowsObject; AName: PChar);
  151.         procedure IDDInstall (var Msg: TMessage); virtual id_First + idd_Install;
  152.         procedure SetUpWindow; virtual;
  153.         procedure WMDrawItem (var Msg: TMessage); virtual wm_First + wm_DrawItem;
  154.     end;
  155.  
  156.     PHotKeyWindow = ^THotKeyWindow;
  157.     THotKeyWindow = object(TWindow)
  158.         CyanBrush, RedBrush : HBrush;
  159.         CyanBkGround        : Boolean;
  160.         WindowNotActive     : Boolean;
  161.         WinMenu             : HMenu;
  162.         constructor Init (AParent: PWindowsObject; AName: PChar);
  163.         destructor Done; virtual;
  164.         function  GetClassName : PChar; virtual;
  165.         procedure GetWindowClass (var AWndClass: TWndClass); virtual;
  166.         procedure IDMEditInstallHotKey (var Msg: TMessage); virtual cm_First + idm_EditInstallHotKey;
  167.         procedure IDMRemoveHotKey (var Msg: TMessage); virtual cm_First + idm_RemoveHotKey;
  168.         procedure IDMAbout (var Msg: TMessage); virtual cm_First + idm_About;
  169.         procedure WMInstallHotKey (var Msg: TMessage); virtual wm_First + wm_InstallHotKey;
  170.         procedure RemoveHotKey; virtual ;
  171.         procedure WMHotKey (var Msg: TMessage); virtual wm_First + wm_HotKey;
  172.         procedure WMTimer  (var Msg: TMessage); virtual wm_First + wm_Timer;
  173.     end;
  174.  
  175.     PHotKeyApplication = ^THotKeyApplication;
  176.     THotKeyApplication = object(TApplication)
  177.         procedure InitMainWindow; virtual;
  178.     end;
  179.  
  180. var
  181.  
  182.     HotKeyRec   : THotKeyStruct;
  183.     HHotKeyHook : HHook;
  184.  
  185. {********** THotKeyApplication **********}
  186.  
  187. procedure THotKeyApplication.InitMainWindow;
  188. begin
  189.     MainWindow := New(PHotKeyWindow, Init(nil, 'HotKey 1 Window - HotKey = None'));
  190. end;
  191.  
  192. {********** Callback function **********}
  193.  
  194. function HotKeyHookProc (nCode: Integer; wParam: Word; lParam: LongInt): LongInt; Export;
  195. {***
  196.  
  197.     When a key is pressed this callback function checks to see if the key pressed (wParam)
  198.     is the same as that stored in the wKey field of the HotKeyStruct record. If it is and the
  199.     appropriate modifier key (Ctrl, Shift +/or Alt) is down a wm_HotKey message is sent to the main
  200.     window. WMHotkey is the end result of the hook. Here it just flashes the background colour
  201.     from cyan to red but it could be used to launch an application or run a menu item.
  202.  
  203.     CallNexthookEx ensures that any unwanted trapped information gets passed onto the
  204.     the next hook in the system;
  205.  
  206. ***}
  207. label
  208.     Exit;
  209. var
  210.     HotKeyWin : HWnd;
  211. begin
  212.     if (nCode = hc_Action) and (HotKeyRec.wKey = WParam) then
  213.     begin
  214.         {*** Exits if the Key was released or if it's a repeat ***}
  215.         if (lParam = $80000000) or (lParam = $40000000) then
  216.             goto Exit;
  217.         {*** Check to see if ALT is part of the shortcut key and pressed ***}
  218.         if HotKeyRec.wMenuKey = 1 then
  219.             if HI(GetKeyState(vk_Menu)) = 0 then goto Exit;
  220.         {*** Check to see if Control is part of the shortcut key and pressed ***}
  221.         if HotKeyRec.wControlKey = 1 then
  222.             if HI(GetKeyState(vk_Control)) = 0 then goto Exit;
  223.         {*** Check to see if Shift is part of the shortcut key and pressed ***}
  224.         if HotKeyRec.wShiftKey = 1 then
  225.             if HI(GetKeyState(vk_Shift)) = 0 then goto Exit;
  226.         HotKeyWin := FindWindow('HotKeyWindow', nil);
  227.         PostMessage(HotKeyWin, wm_HotKey, 0, 0);
  228.     end;
  229.  
  230.     Exit:
  231.         HotKeyHookProc := CallNextHookEx(HHotKeyHook, nCode, WParam,